﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using E2_WEBSITE.Code.Admin;
using BasicFrame.WebControls;
using E2_WEBSITE.Code;

namespace E2_WEBSITE.cp.admin.site
{
    public partial class adsdetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (Session["Authentication"] == null || Convert.ToInt32(Session["Authentication"]) != 0)
            {
                WebMsgBox.Show("Bạn phải là admin để có thể sử dụng chức năng này.");
                return;
            }
            try
            {
                if (formAdsDetail.DataItemCount > 0)
                {
                    if (Page.IsValid)
                    {
                        int aid = int.Parse(Page.Request["AdsId"]);
                        AdsController controller = new AdsController();
                        AdsInfo ads = new AdsInfo(
                            (formAdsDetail.FindControl("dtpStart") as BDPLite).SelectedDate,
                            (formAdsDetail.FindControl("dtpEnd") as BDPLite).SelectedDate,
                            int.Parse((formAdsDetail.FindControl("tbPosition") as TextBox).Text),
                            int.Parse((formAdsDetail.FindControl("tbPrice") as TextBox).Text),
                            (formAdsDetail.FindControl("tbImage") as TextBox).Text,
                            (formAdsDetail.FindControl("tbNav") as TextBox).Text);

                        if (controller.UpdateAds(
                            aid,
                            int.Parse((formAdsDetail.FindControl("ddlUser") as DropDownList).SelectedValue),
                            ads))
                        {
                            Page.Response.Redirect("~/cp/admin/site/adsmanagement.aspx");
                        }
                    }
                }
            }
            catch (Exception)
            {
                Response.Redirect(Utilities.ErrorPagePath);
                throw;
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Page.Response.Redirect("~/cp/admin/site/adsmanagement.aspx");
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {

            if (Session["Authentication"] == null || Convert.ToInt32(Session["Authentication"]) != 0)
            {
                WebMsgBox.Show("Bạn phải là admin để có thể sử dụng chức năng này.");
                return;
            }
            try
            {
                if (formAdsDetail.DataItemCount > 0)
                {
                    if (Page.IsValid)
                    {
                        int aid = int.Parse(Page.Request["AdsId"]);
                        AdsController controller = new AdsController();

                        if (controller.DeleteAds(aid))
                        {
                            Page.Response.Redirect("~/cp/admin/site/adsmanagement.aspx");
                        }
                    }
                }
            }
            catch (Exception)
            {
                Response.Redirect(Utilities.ErrorPagePath);
                throw;
            }
        }
    }
}
